<html>
<head><meta charset="utf-8"><title>Uniqueness of mutable references · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html">Uniqueness of mutable references</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="163542543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163542543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163542543">(Apr 17 2019 at 08:47)</a>:</h4>
<p>How do y'all feel about code like this?</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">test</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">(</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="p">))</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="kt">i32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">xraw</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x2</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="p">(</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// we just read, but this still invalidates x2 and hence xraw</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">xraw</span><span class="p">;</span><span class="w"> </span><span class="c1">// UB?</span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>I am contemplating whether this should be UB or not. I feel the model gets much nicer if this is UB: when <code>x2</code> gets invalidated, we should also invalidate what got derived from <code>x2</code>. However, then the following is currently also UB:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">test</span><span class="p">(</span><span class="n">x</span>: <span class="kp">&amp;</span><span class="nc">mut</span><span class="w"> </span><span class="p">(</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="p">))</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="kt">i32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">xraw</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="p">(</span><span class="kt">i32</span><span class="p">,</span><span class="w"> </span><span class="kt">i32</span><span class="p">);</span><span class="w"> </span><span class="c1">// implicitly a (&amp;mut *x)</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// we just read, but this still invalidates x2 and hence xraw</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">xraw</span><span class="p">;</span><span class="w"> </span><span class="c1">// UB?</span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>Cc <span class="user-mention" data-user-id="116118">@Matthew Jasper</span></p>



<a name="163542887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163542887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163542887">(Apr 17 2019 at 08:52)</a>:</h4>
<p>I would guess that also in LLVM, when a pointer can no longer be used because its <code>noalias</code> scope has ended, pointers derived from it cannot be used any more either? Cc <span class="user-mention" data-user-id="126804">@Ariel Ben-Yehuda</span></p>



<a name="163545263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163545263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163545263">(Apr 17 2019 at 09:32)</a>:</h4>
<p>dang, this pattern appears in libstd... not explicitly but through the desugaring of <code>x as *mut _</code> as <code>(&amp;mut *x) as *mut _</code>. so it has to be allowed. :/</p>



<a name="163546186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163546186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163546186">(Apr 17 2019 at 09:47)</a>:</h4>
<p>I could try to do some syntactic hacks to make <code>x as *mut _</code> not have retagging from the implicit reborrow, but maybe we dont want to be quite so sensitive to reborrows anyway? No idea what that'll mean for LLVM compatibility though.</p>



<a name="163547178"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547178" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547178">(Apr 17 2019 at 10:04)</a>:</h4>
<p>but this does mean that if we actually tracked raw pointer identity like LLVM does, you could not cast the same mutable ref to a raw pointer twice and then use both of them. the implicit reborrow of the second cast would certainly have to kill the preexisting raw ptr, after all we do want to know for sure that our newly created mutable reference is unique -- right? do we?</p>



<a name="163547593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547593">(Apr 17 2019 at 10:11)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> do you want different semantics between <code>let _val = x.1</code> and <code>x.1 = 4;</code> ?</p>



<a name="163547671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547671">(Apr 17 2019 at 10:12)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> yes, that is unavoidable.</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;*</span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// can still read y after *reading* x (accepted by borrow checker)</span>
<span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// but certainly not after *writing* x</span>
</pre></div>



<a name="163547706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547706">(Apr 17 2019 at 10:13)</a>:</h4>
<p>does that also apply if I write through the pointer first, and then read through the reference ?</p>



<a name="163547775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547775">(Apr 17 2019 at 10:14)</a>:</h4>
<p>...?</p>



<a name="163547827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547827">(Apr 17 2019 at 10:15)</a>:</h4>
<p>please post code, I have no idea what you mean</p>



<a name="163547831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547831">(Apr 17 2019 at 10:15)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"> </span><span class="c1">// can still read y after *reading* x (accepted by borrow checker)</span>
<span class="o">*</span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"> </span><span class="c1">// is this ok?</span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// if so, is this ok?</span>
</pre></div>



<a name="163547885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547885">(Apr 17 2019 at 10:16)</a>:</h4>
<p>eh, you are asking if you can write to a shared ref? no you cannot^^</p>



<a name="163547898"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547898" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547898">(Apr 17 2019 at 10:16)</a>:</h4>
<p>sorry, wrong type of <code>y</code></p>



<a name="163547915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547915">(Apr 17 2019 at 10:17)</a>:</h4>
<p>by symmetry I'd expect that <code>y</code> can be used until <code>x</code> is written through</p>



<a name="163547935"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547935" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547935">(Apr 17 2019 at 10:17)</a>:</h4>
<p>so the two loads of <code>x</code> cannot be hoisted, because one can write through y, even though x is <code>noalias</code> ?</p>



<a name="163547996"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163547996" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163547996">(Apr 17 2019 at 10:18)</a>:</h4>
<p>so yes I think your code should be okay. but the question above is about</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">;</span><span class="w"> </span><span class="c1">// &lt;- mind the extra reborrow</span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// kills the reborrow (which is unique) and hence kills y?</span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">;</span><span class="w"></span>
<span class="o">*</span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="p">;</span><span class="w"></span>
</pre></div>



<a name="163548020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548020">(Apr 17 2019 at 10:18)</a>:</h4>
<p><code>x</code> being noalias means it doesnt alias with stuff that was created earlier. but it can alias with new pointers created from <code>x</code>.</p>



<a name="163548040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548040">(Apr 17 2019 at 10:19)</a>:</h4>
<p>ah</p>



<a name="163548198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548198">(Apr 17 2019 at 10:21)</a>:</h4>
<p>so i have no idea if that should be UB</p>



<a name="163548227"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548227" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548227">(Apr 17 2019 at 10:21)</a>:</h4>
<p>if you have to pop up the stack to find out if you can dereference <code>x</code>, and then you dereference <code>y</code>, then in the current model it would be UB IIUC because <code>y</code> has been popped from the stack</p>



<a name="163548298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548298">(Apr 17 2019 at 10:22)</a>:</h4>
<p>but with implicit reborrows this means</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y1</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">;</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">y2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="kt">i32</span><span class="p">;</span><span class="w"></span>
<span class="c1">// can we still use y1?</span>
</pre></div>


<p>is an open question... the <code>&amp;mut *x</code> in the <code>let y2</code> line asserts that it is a unique pointer (a fresh noalias pointer), which I guess should mean that y1 is not allowed to be used any more</p>



<a name="163548320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548320">(Apr 17 2019 at 10:23)</a>:</h4>
<p>if one wanted to not make it UB, one would need to pop stuff from the stack to check for <code>x</code>, but put derived stuff from <code>x</code> on the top of the stack somewhere else (e.g. a second stack), and the algo would be more complicated</p>



<a name="163548338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548338">(Apr 17 2019 at 10:23)</a>:</h4>
<p>well the current WIP implementation somewhat gives up on the stack thing</p>



<a name="163548385"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548385" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548385">(Apr 17 2019 at 10:24)</a>:</h4>
<p>does making it UB enable any optimizations ?</p>



<a name="163548391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548391">(Apr 17 2019 at 10:24)</a>:</h4>
<p>(I am working on somewhat of a redesign of stacked borrows)</p>



<a name="163548406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548406">(Apr 17 2019 at 10:24)</a>:</h4>
<p>and basically removes from the stack what is incompatible with the access -- so the implicit reborrow would get removed but the raw ptr created from <em>that</em> would not</p>



<a name="163548408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548408">(Apr 17 2019 at 10:24)</a>:</h4>
<blockquote>
<p>does making it UB enable any optimizations ?</p>
</blockquote>
<p>no idea^^</p>



<a name="163548429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548429">(Apr 17 2019 at 10:25)</a>:</h4>
<p>so from a user pov, this appears to be something that I might want to do</p>



<a name="163548441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548441">(Apr 17 2019 at 10:25)</a>:</h4>
<p>if this is UB, i'd write more complicated code to achieve the same thing to avoid the UB</p>



<a name="163548447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548447">(Apr 17 2019 at 10:25)</a>:</h4>
<p>it would be nice to know if there are any more advantages than just making stacked borrows simpler</p>



<a name="163548497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163548497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163548497">(Apr 17 2019 at 10:26)</a>:</h4>
<p>not that a simple stacked borrows model isn't a goal worth pursuing, but that's at tension with requiring users to write more complex code</p>



<a name="163551616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163551616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163551616">(Apr 17 2019 at 11:23)</a>:</h4>
<blockquote>
<p>it would be nice to know if there are any more advantages than just making stacked borrows simpler</p>
</blockquote>
<p>it's not just about simpler. for the case of creating multiple mutable references, I dont see any way to to do that with the current constraints.</p>



<a name="163551633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163551633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163551633">(Apr 17 2019 at 11:23)</a>:</h4>
<p>however, what <em>could</em> work is being less aggressive about retagging: maybe a simple assignment-style reborrow (<code>x = &amp;[mut]*y</code>) should really truly be a NOP, even in stacked borrows</p>



<a name="163551645"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163551645" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163551645">(Apr 17 2019 at 11:23)</a>:</h4>
<p>I think that would solve all these problems</p>



<a name="163551696"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163551696" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163551696">(Apr 17 2019 at 11:24)</a>:</h4>
<p>at the cost of making the logic to emit retagging more complicated</p>



<a name="163552140"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness%20of%20mutable%20references/near/163552140" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Uniqueness.20of.20mutable.20references.html#163552140">(Apr 17 2019 at 11:32)</a>:</h4>
<p>it would be nice though to at least capture the explicit reborrows the user wrote... hm</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>